package com.sinochem.yunlian.ship.base.rest;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.sinochem.yunlian.ship.api.vo.bs.BsNodeVO;
import com.sinochem.yunlian.ship.base.biz.BsNodeBiz;
import com.sinochem.yunlian.ship.base.entity.BsNode;
import com.sinochem.yunlian.ship.common.constant.RedisKey;
import com.sinochem.yunlian.ship.common.enums.PortType;
import com.sinochem.yunlian.ship.common.enums.ResponseCode;
import com.sinochem.yunlian.ship.common.msg.RestResponse;
import com.sinochem.yunlian.ship.common.rest.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("bsNode")
public class BsNodeController extends BaseController<BsNodeBiz,BsNode> {

    /**
     * 非必填节点
     */
    public static final int NOT_REQUIRED_NODE = 0;
    /**
     * 必填节点
     */
    public static final int REQUIRED_NODE = 1;

    @Autowired
    private BsNodeBiz  bsNodeBiz;

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 节点列表：
     *
     *  required  0 非必填  1 必填
     *  portType  0 装货港  1 卸货港 不传港口类型，则查询全部
     * @return
     */
    @RequestMapping(value = "/getNodesByCondition",method = RequestMethod.POST)
    public RestResponse getNodeList(@RequestBody Map inMap){
        if(!inMap.containsKey("portType") || !inMap.containsKey("required")){
            return RestResponse.response(ResponseCode.PARAM_ERROR.getCode(),"参数不正确");
        }
        String _required = inMap.get("required").toString();
        String _portType = inMap.get("portType").toString();
        Integer required = new Integer(_required);
        Integer portType = null;
        if(StringUtils.isNotBlank(_portType)){
            portType = new Integer(_portType);
        }
        if(portType != null && (portType.intValue() < PortType.FROM_PORT.getCode()
                          || portType.intValue() > PortType.TO_PORT.getCode())){
            return RestResponse.response(ResponseCode.PARAM_ERROR.getCode(),"港口类型不正确");
        }
        if(required == null || required.intValue() < NOT_REQUIRED_NODE
                ||  required.intValue() > REQUIRED_NODE){
            return RestResponse.response(ResponseCode.PARAM_ERROR.getCode(),"是否必填类型不能为空");
        }
        boolean requiredFlag = required.intValue() == REQUIRED_NODE ? true : false;
        String redisKey = null;
        List<BsNodeVO>  bsNodeVOS = null;
        if(required.intValue() == NOT_REQUIRED_NODE){
            if(portType == null){
                bsNodeVOS =  getBsNodeVOS(requiredFlag, portType,RedisKey.NOT_REQUIRED_KEY_NODE_ALL);
            }else if(portType.intValue() == PortType.FROM_PORT.getCode()){
                bsNodeVOS =  getBsNodeVOS(requiredFlag, portType,RedisKey.NOT_REQUIRED_KEY_NODE_FORM);
            }else {
                bsNodeVOS =  getBsNodeVOS(requiredFlag, portType,RedisKey.NOT_REQUIRED_KEY_NODE_TO);
            }
        }else{
            if(portType == null){
                bsNodeVOS =  getBsNodeVOS(requiredFlag, portType,RedisKey.REQUIRED_KEY_NODE_ALL);
            }else if(portType.intValue() == PortType.FROM_PORT.getCode()){
                bsNodeVOS =  getBsNodeVOS(requiredFlag, portType,RedisKey.REQUIRED_KEY_NODE_FROM);
            }else {
                bsNodeVOS =  getBsNodeVOS(requiredFlag, portType,RedisKey.REQUIRED_KEY_NODE_TO);
            }
        }
        return RestResponse.success(bsNodeVOS);
    }

    private List<BsNodeVO> getBsNodeVOS(boolean isRequired,Integer portType, String redisKey) {
        if (redisTemplate.hasKey(redisKey)) {
            String needNodes = (String) redisTemplate.boundValueOps(redisKey).get();
            List<BsNodeVO> nodes = JSONObject.parseArray(needNodes,BsNodeVO.class);
            return nodes;
        }
        if(isRequired){
            List<BsNode> nodes = bsNodeBiz.findAllRequiredNodes(portType);
            List<BsNodeVO> bsNodeVOS = new ArrayList<>();
            for(BsNode bsNode : nodes){
                BsNodeVO bsNodeVO = new BsNodeVO();
                BeanUtils.copyProperties(bsNode,bsNodeVO);
                bsNodeVOS.add(bsNodeVO);
            }
            String needNodes = JSONObject.toJSONString(bsNodeVOS);
            BoundValueOperations valueOperations =redisTemplate.boundValueOps(redisKey);
            valueOperations.set(needNodes);
            return bsNodeVOS;
        }else{
            List<BsNode> nodes = bsNodeBiz.findAllNotRequiredNodes(portType);
            List<BsNodeVO> bsNodeVOS = new ArrayList<>();
            for(BsNode bsNode : nodes){
                BsNodeVO bsNodeVO = new BsNodeVO();
                BeanUtils.copyProperties(bsNode,bsNodeVO);
                bsNodeVOS.add(bsNodeVO);
            }
            String needNodes = JSONObject.toJSONString(bsNodeVOS);
            BoundValueOperations valueOperations =redisTemplate.boundValueOps(redisKey);
            valueOperations.set(needNodes);
            return bsNodeVOS;
        }
    }

    /**
     * pageNum,pageSize
     * find vo
     * 运营
     * @param params
     * @return
     */
    @RequestMapping("/om/nodeList")
    public RestResponse findNodes(@RequestBody Map params){
        Page<BsNode> data =bsNodeBiz.findNodeByCondition(params);
        Map redata = new HashMap();
        redata.put("total",data.getTotal());
        redata.put("rows",data.getResult());
        return RestResponse.success(redata);
    }
}